//
//  Dc_XinGeViewController.m
//  艾薇贝儿
//
//  Created by dllo on 15/9/17.
//  Copyright (c) 2015年 dllo－－邴鹏超. All rights reserved.
//

#import "Dc_XinGeViewController.h"
#import "DcAFNetWorkTool.h"
#import "DcFirstModel.h"
#import "DcSongsModel.h"
#import "UIImageView+WebCache.h"
#import "Dc_HuaViewController.h"
#import "Dc_OuMeiViewController.h"
#import "Dc_HanYuViewController.h"
#import "Dc_RiYuViewController.h"
#import "AppDelegate.h"

@interface Dc_XinGeViewController ()<UIScrollViewDelegate>
{
    UIScrollView * _scrollView;
    UIScrollView * _conScrollView;
    NSMutableArray * _array;
    NSArray * _imageArray;
    NSMutableArray * _butarray;
    UIView * _view;
    UITableView * _tableView;
    UISegmentedControl * _segMen;
    UIImageView * _headImage;
    UIScrollView * _scro;
    UIImageView * _view1;
     UIImageView * _view2;
     UIImageView * _view3;
     UIImageView * _view4;
    NSMutableArray * _urlArray;
    NSMutableArray * _picArray;
    DcSongsModel * mod;
    DcFirstModel * mod2;
}

@end

@implementation Dc_XinGeViewController
{
    CGFloat xlen;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor cyanColor]];
    [self setTitle:@"新歌榜"];
    xlen=self.view.frame.size.width;
  
    
    _scrollView  = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 214*OffHeight, 375*OffWidth, 40*OffHeight)];
    [_scrollView  setContentSize:CGSizeMake(xlen/4, 0)];
    [_scrollView setDelegate:self];
    [_scrollView setBackgroundColor:[UIColor lightGrayColor]];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [self.view addSubview:_scrollView];
    
    _array=[NSMutableArray arrayWithObjects:@"华语",@"欧美",@"韩语",@"日语",nil];
  
   _segMen = [[UISegmentedControl alloc] initWithItems:_array];
    _segMen.frame = CGRectMake(0, 0, xlen, 40);
    _segMen.tintColor = [UIColor clearColor];
    _segMen.selectedSegmentIndex = 0;
    [_scrollView addSubview:_segMen];
    [_segMen release];
    
    NSDictionary * selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],NSForegroundColorAttributeName:[UIColor redColor]};
    [_segMen setTitleTextAttributes:selectedTextAttributes forState:(UIControlStateSelected)];
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: [UIColor colorWithRed:65/255.0 green:105/255.0 blue:1 alpha:1]};
    [_segMen setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    
    
    [_segMen addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    
    _conScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 254*OffHeight, xlen,413*OffHeight )];
    [_conScrollView setContentSize:CGSizeMake(self.view.frame.size.width*4, 0)];
    _conScrollView.bounces=NO;
    _conScrollView.clipsToBounds = NO;
    [_conScrollView setPagingEnabled:YES];
    [_conScrollView setTag:1001];
    [_conScrollView setDelegate:self];
    [self.view addSubview:_conScrollView];
    
    for (int i=0; i<4; i++) {
        CGFloat red =arc4random()% 256;
        CGFloat green =arc4random()% 256;
        CGFloat blue =arc4random()% 256;
        _view =[[UIView alloc]initWithFrame:CGRectMake(xlen*i, 0, xlen, 413*OffHeight)];
        
        [_view setBackgroundColor:[UIColor colorWithRed:red/256. green:green/256. blue:blue/256. alpha:1]];
        [_view setTag:i];
        [_conScrollView addSubview:_view];
        [_view release];
    }
    _scro=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64*OffHeight, xlen, 150*OffHeight)];
    [_scro setContentSize:CGSizeMake(self.view.frame.size.width*4, 0)];
    _scro.clipsToBounds = YES;
    [_scro setPagingEnabled:YES];
    [_scro setDelegate:self];
    [self.view addSubview:_scro];
    
  
    _view1 =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, xlen, 150*OffHeight)];
  
    [_scro addSubview:_view1];
    [_view1 release];
        
  
    
    _view2 =[[UIImageView alloc]initWithFrame:CGRectMake(xlen, 0, xlen, 150*OffHeight)];
 
    [_scro addSubview:_view2];
    [_view2 release];
    
    
    _view3 =[[UIImageView alloc]initWithFrame:CGRectMake(xlen*2, 0, xlen, 150*OffHeight)];
 
    [_scro addSubview:_view3];
    [_view3 release];
    
    
    _view4 =[[UIImageView alloc]initWithFrame:CGRectMake(xlen*3, 0, xlen, 150*OffHeight)];
  
    [_scro addSubview:_view4];
    [_view4 release];
    
    
    

    Dc_HuaViewController * hua=[[Dc_HuaViewController alloc]init];
    hua.view.frame=CGRectMake(0, 0, xlen, 413*OffHeight);
    
    [_conScrollView addSubview:hua.view];
    [self addChildViewController:hua];
    
    
    Dc_OuMeiViewController * ouMei=[[Dc_OuMeiViewController alloc]init];
    ouMei.view.frame=CGRectMake(xlen, 0, xlen, 413*OffHeight);
    [_conScrollView addSubview:ouMei.view];
    [self addChildViewController:ouMei];
    
   
    Dc_HanYuViewController * hanYu=[[Dc_HanYuViewController alloc]init];
    hanYu.view.frame=CGRectMake(xlen*2, 0, xlen, 413*OffHeight);
    [_conScrollView addSubview:hanYu.view];
    [self addChildViewController:hanYu];
    
    
    Dc_RiYuViewController * riYu=[[Dc_RiYuViewController alloc]init];
    riYu.view.frame = CGRectMake(xlen*3, 0, xlen, 413*OffHeight);
    [self addChildViewController:riYu];
    [_conScrollView addSubview:riYu.view];
    
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self http1];
    }
    return self;
}
-(void)http1{
    _picArray=[[NSMutableArray alloc]init];
    
    [DcAFNetWorkTool getUrl:@"http://api.songlist.ttpod.com/songlists/300002376?f=0&os=4.4.2&alf=10001791&imei=111111111111111&from=android&resolution=480x854&net=2&api_version=1.0&agent=none&v=v8.1.1.2015081317&utdid=VV2s9r3zhNgDALDQVlZ6OMqc&longitude=0.0&user_id=0&latitude=0.0&language=zh" body:nil result:DcJSON headerFlile:nil success:^(id result) {
        NSMutableDictionary * dic1=[result objectForKey:@"image"];
        mod=[[DcSongsModel alloc]init];
        [mod setValuesForKeysWithDictionary:dic1];
            [ _view1   sd_setImageWithURL:[NSURL  URLWithString:mod.pic]placeholderImage:[UIImage imageNamed:@"jia.jpg"]];
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
    [DcAFNetWorkTool getUrl:@"http://api.songlist.ttpod.com/songlists/300002375?f=0&os=4.4.2&alf=10001791&imei=111111111111111&from=android&resolution=480x854&net=2&api_version=1.0&agent=none&v=v8.1.1.2015081317&utdid=VV2s9r3zhNgDALDQVlZ6OMqc&longitude=0.0&user_id=0&latitude=0.0&language=zh" body:nil result:DcJSON headerFlile:nil success:^(id result) {
        
        NSMutableDictionary * dic1=[result objectForKey:@"image"];
        mod=[[DcSongsModel alloc]init];
        [mod setValuesForKeysWithDictionary:dic1];

        [ _view2   sd_setImageWithURL:[NSURL  URLWithString:mod.pic] placeholderImage:[UIImage imageNamed:@"jia.jpg"]];

        [_tableView reloadData];
    } failure:^(NSError *error) {
    
        
    }];
    
    [DcAFNetWorkTool getUrl:@"http://api.songlist.ttpod.com/songlists/97258652?f=0&os=4.4.2&alf=10001791&imei=111111111111111&from=android&resolution=480x854&net=2&api_version=1.0&agent=none&v=v8.1.1.2015081317&utdid=VV2s9r3zhNgDALDQVlZ6OMqc&longitude=0.0&user_id=0&latitude=0.0&language=zh" body:nil result:DcJSON headerFlile:nil success:^(id result) {
        
        NSMutableDictionary * dic1=[result objectForKey:@"image"];
        mod=[[DcSongsModel alloc]init];
        [mod setValuesForKeysWithDictionary:dic1];
        
        [ _view3   sd_setImageWithURL:[NSURL  URLWithString:mod.pic] placeholderImage:[UIImage imageNamed:@"jia.jpg"]];
        
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
    
    
    [DcAFNetWorkTool getUrl:@"http://api.songlist.ttpod.com/songlists/97258648?f=0&os=4.4.2&alf=10001791&imei=111111111111111&from=android&resolution=480x854&net=2&api_version=1.0&agent=none&v=v8.1.1.2015081317&utdid=VV2s9r3zhNgDALDQVlZ6OMqc&longitude=0.0&user_id=0&latitude=0.0&language=zh" body:nil result:DcJSON headerFlile:nil success:^(id result) {
        
        NSMutableDictionary * dic1=[result objectForKey:@"image"];
        mod=[[DcSongsModel alloc]init];
        [mod setValuesForKeysWithDictionary:dic1];
        
        [ _view4   sd_setImageWithURL:[NSURL  URLWithString:mod.pic] placeholderImage:[UIImage imageNamed:@"jia.jpg"]];
        
        [_tableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}



-(void)segmentedControlAction:(UISegmentedControl *)seg
    {
    NSInteger index = seg.selectedSegmentIndex;
    _conScrollView.contentOffset = CGPointMake(xlen*index, 0);
        _scro.contentOffset=CGPointMake(xlen*index, 0);
        
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _conScrollView) {
      _segMen.selectedSegmentIndex = scrollView.contentOffset.x/xlen;
        _scro.contentOffset=scrollView.contentOffset;

    }
    if (scrollView == _scro) {
        _segMen.selectedSegmentIndex = scrollView.contentOffset.x/xlen;
        _conScrollView.contentOffset=scrollView.contentOffset;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
